package com.derbysoft.dswitch.adapter.ihg.webservice.handler;

import com.derbysoft.dswitch.adapter.ihg.common.domain.*;
import com.derbysoft.dswitch.adapter.ihg.common.log.TransactionLogger;
import com.derbysoft.dswitch.adapter.ihg.common.repository.impl.HotelTransactionLog;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by: jason
 * Date: 2012-06-08
 */
public abstract class AbstractWebServiceHandler {
    protected HotelTransactionLog createHotelTransactionLog(Direction direction, String message) {
        return HotelTransactionLog.of(direction, message);
    }

    protected List<TransactionDetail> toAvailDetails(List<HotelTransactionLog> hotelTransactionLogs) {
        ArrayList<TransactionDetail> transactionDetails = new ArrayList<TransactionDetail>();
        for (HotelTransactionLog log : hotelTransactionLogs) {
            transactionDetails.add(createLogDetail(TransactionLogger.getAvailLogger().get(), log.getMessage(), log.getDirection()));
        }
        return transactionDetails;
    }

    protected List<ReservationDetail> toReservationDetails(List<HotelTransactionLog> hotelTransactionLogs) {
        ArrayList<ReservationDetail> reservationDetails = new ArrayList<ReservationDetail>();
        for (HotelTransactionLog log : hotelTransactionLogs) {
            reservationDetails.add(createLogDetail(TransactionLogger.getReservationLogger().get(), log.getMessage(), log.getDirection()));
        }
        return reservationDetails;
    }

    private static TransactionDetail createLogDetail(TransactionLog transactionLog, String detail, Direction direction) {
        TransactionDetail transactionDetail = new TransactionDetail();
        transactionDetail.setTransactionLog(transactionLog);
        transactionDetail.setDetail(detail);
        transactionDetail.setDirection(direction);
        return transactionDetail;
    }

    private static ReservationDetail createLogDetail(ReservationLog reservationLog, String detail, Direction direction) {
        ReservationDetail reservationDetail = new ReservationDetail();
        reservationDetail.setReservationLog(reservationLog);
        reservationDetail.setDetail(detail);
        reservationDetail.setDirection(direction);
        return reservationDetail;
    }
}
